package com.jy.adscm.operation.aftersales.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.BaseQuery;
import com.jy.adscm.operation.aftersales.bean.ShSettingParts;
import com.jy.adscm.operation.aftersales.service.ShSettingPartsService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-28
 */
@RestController
@RequestMapping("/adscm/operation/aftersales/shSettingParts")
public class ShSettingPartsController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    ShSettingPartsService shSettingPartsService;

    /**
     * 新增客户信息
     * @param shSettingParts
     * @param request
     * @return
     */
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody ShSettingParts shSettingParts, HttpServletRequest request)throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        shSettingParts.setCreator(token.getUser().getId().toString());
        shSettingParts.setCreateTime(new Date());
        boolean insert = shSettingPartsService.insert(shSettingParts);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改用户信息
     * @param shSettingParts
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody ShSettingParts shSettingParts, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        shSettingParts.setCreator(token.getUser().getId().toString());
        shSettingParts.setCreateTime(new Date());
        boolean result = shSettingPartsService.updateById(shSettingParts);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer  id, HttpServletRequest request) throws AdscmSystemException {
        boolean result = shSettingPartsService.deleteById(id);
        return  new SimpleCoreResponse("200","删除成功",result);
    }

    /**
     * 根据查询条件查询故障信息
     * @param baseQuery
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll(@RequestBody BaseQuery baseQuery, HttpServletRequest request) throws  AdscmSystemException{
        Page<ShSettingParts> page =new Page<>(baseQuery.getCurrentPage(),baseQuery.getPageSize());
        page = shSettingPartsService.selectAll(page,baseQuery);

        return  new SimpleCoreResponse("200","查询成功",page);
    }


}

